package com.qs.eggs.view.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.MainActivity;
import com.qs.eggs.R;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.utils.CityDialogHelper;
import com.qs.eggs.view.fragment.CategoryFragment;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class AddAddressNewActivity extends Activity implements View.OnClickListener{
    private String province,city,area,village,custname,phone,address,id,provinceid,cityid,areaid,villageid,isdefault;
    private EditText dizhi;
    private TextView tvProvince,tvCity,tvCounty,tvCommunity;
    private LinearLayout mProvince,mCity,mCounty,mCommunity;
    private Button save;
    private TextView tvForIdP,tvForIdCity,tvForIdCounty,tvForIdCommunity,tvForIdVillage;
    private String ProvinceId="",CityId="",CountyId="",CommunityId="";
    private TextView name,number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address_new);
        province=getIntent().getStringExtra("province");
        city=getIntent().getStringExtra("city");
        area=getIntent().getStringExtra("area");
        village=getIntent().getStringExtra("village");
        custname=getIntent().getStringExtra("custname");
        phone=getIntent().getStringExtra("phone");
        address=getIntent().getStringExtra("address");
        id=getIntent().getStringExtra("id");
        provinceid=getIntent().getStringExtra("provinceid");
        cityid=getIntent().getStringExtra("cityid");
        areaid=getIntent().getStringExtra("areaid");
        villageid=getIntent().getStringExtra("villageid");
        isdefault=getIntent().getStringExtra("isdefault");
        initView();
    }
    private void initView(){
        name=(TextView)findViewById(R.id.et_name);
        number=(TextView)findViewById(R.id.et_phone);
        dizhi=(EditText)findViewById(R.id.et_address);
//        Editable etext = dizhi.getText();
//        dizhi.setSelection(etext.length());

        tvProvince=(TextView)findViewById(R.id.tv_province);
        tvCity=(TextView)findViewById(R.id.tv_city);
        tvCounty=(TextView)findViewById(R.id.tv_county);
        tvCommunity=(TextView)findViewById(R.id.tv_community);

        tvForIdP=(TextView)findViewById(R.id.tv_forid_p);
        tvForIdCity=(TextView)findViewById(R.id.tv_forid_city);
        tvForIdCounty=(TextView)findViewById(R.id.tv_forid_county);
        tvForIdCommunity=(TextView)findViewById(R.id.tv_forid_ommunity);

        mProvince=(LinearLayout)findViewById(R.id.lly_province);
        mCity=(LinearLayout)findViewById(R.id.lly_city);
        mCounty=(LinearLayout)findViewById(R.id.lly_county);
        mCommunity=(LinearLayout)findViewById(R.id.lly_community);
        save=(Button)findViewById(R.id.save);

        mProvince.setOnClickListener(this);
        mCity.setOnClickListener(this);
        mCounty.setOnClickListener(this);
        mCommunity.setOnClickListener(this);
        save.setOnClickListener(this);

        name.setText(custname);
        number.setText(phone);
        dizhi.setText(address);
        tvProvince.setText(province);
        tvCity.setText(city);
        tvCounty.setText(area);
        tvCommunity.setText(village);

        tvForIdP.setText(provinceid);
        tvForIdCity.setText(cityid);
        tvForIdCounty.setText(areaid);
        tvForIdCommunity.setText(villageid);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui:
                AddAddressNewActivity.this.finish();
                break;
            case R.id.lly_province:

                HttpApi.getProvince(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mProvincesList=new ArrayList<String>();
                            ArrayList<String> mProvincesListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mProvincesList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mProvincesListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(AddAddressNewActivity.this,mProvincesList,mProvincesListId,tvProvince,tvForIdP);
                            tvCity.setText("");
                            tvCounty.setText("");
                            tvCommunity.setText("");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
            //加载市列表
            case R.id.lly_city:
                if(tvProvince.getText().toString().trim().length()==0){
                    Toast.makeText(AddAddressNewActivity.this,"请先选择省",Toast.LENGTH_SHORT).show();
                    return;
                }
                provinceid=tvForIdP.getText().toString();
                HttpApi.getCity(provinceid,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mCityList=new ArrayList<String>();
                            ArrayList<String> mCityListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCityList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCityListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(AddAddressNewActivity.this,mCityList,mCityListId,tvCity,tvForIdCity);
                            tvCounty.setText("");
                            tvCommunity.setText("");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }


                });
                break;

            //加载县列表
            case R.id.lly_county:
                if(tvCity.getText().toString().trim().length()==0){
                    Toast.makeText(AddAddressNewActivity.this,"请先选择市",Toast.LENGTH_SHORT).show();
                    return;
                }
                cityid=tvForIdCity.getText().toString();
                HttpApi.getCounty(cityid,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mCountyList=new ArrayList<String>();
                            ArrayList<String> mCountyListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCountyList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCountyListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(AddAddressNewActivity.this,mCountyList,mCountyListId,tvCounty,tvForIdCounty);
                            tvCommunity.setText("");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;

            //加载小区列表
            case R.id.lly_community:
                if(tvCounty.getText().toString().trim().length()==0){
                    Toast.makeText(AddAddressNewActivity.this,"请先选择县",Toast.LENGTH_SHORT).show();
                    return;
                }
               areaid=tvForIdCounty.getText().toString();
                HttpApi.getCommunity(areaid,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mCommunityList=new ArrayList<String>();
                            ArrayList<String> mCommunityListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCommunityList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCommunityListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(AddAddressNewActivity.this,mCommunityList,mCommunityListId,tvCommunity,tvForIdCommunity);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                });

                break;
            case R.id.save:
                if (tvProvince.getText().equals("")||tvCity.getText().equals("")||tvCounty.getText().equals("")||tvCommunity.getText().equals("")|| TextUtils.isEmpty(dizhi.getText().toString())){
                    Toast.makeText(AddAddressNewActivity.this,"信息不能为空",Toast.LENGTH_SHORT).show();
                }else{
                    xiugai();
                }

                break;
        }
    }
    //确认修改
    private void xiugai(){
        villageid=tvForIdCommunity.getText().toString();
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("provinceid",provinceid);
        map.put("cityid",cityid);
        map.put("areaid",areaid);
        map.put("villageid",villageid);
        map.put("address",dizhi.getText().toString());
        map.put("id",id);
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"mallAddressManage?action=updateCustAddress")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("修改地址=="+response);
                        if (s.equals("\"true\"")){
                            Toast.makeText(AddAddressNewActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                            AddAddressNewActivity.this.finish();
                        }
                    }

                });

    }
}
